package com.soccernews.main;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;






import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class Details extends Activity {
	ListView list;
	private List<Message> messages;
	ProgressDialog pd;
	private Object data = null;
	Elements content;
	Bitmap bitmap;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_screen);
        //this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", true, false);

    	
        new DownloadTask().execute();
        
    }
    
    private class DownloadTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args) {
            Log.i("MyApp", "Background thread starting");
            try {
            Document doc = Jsoup.connect(getIntent().getExtras().getString("param")).get();
            
			
			
			 content = doc.select("tr");

			
			
		      Element img = doc.select("div#anc-op img").first();
		    String imgSrc = img.attr("src");
		    
		     bitmap = BitmapFactory.decodeStream((InputStream)new URL(imgSrc).getContent());
		     
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
            return content;
        }

        protected void onPostExecute(Object result) {
            // Pass the result data back to the main activity
        	Details.this.data = result;
        	if (content == null)
        		new DownloadTask().execute();
        	else{
        	setContentView(R.layout.detail);
        	ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        	TextView textView = (TextView) findViewById(R.id.detail_view);
        	textView.setText(content.get(3).text());
        	imageView.setImageBitmap(bitmap);
        	if(getIntent().getExtras() !=null ){
           	 TextView title = (TextView) findViewById(R.id.title1);
           	 title.setText(getIntent().getExtras().getString("param2"));
        	}
   		}	
        	if (Details.this.pd != null) {
            	Details.this.pd.dismiss();
            }
        }
    }
      

        
       
        
        	
    
    

	
}